package com.redare.dev.smart.generator.service.provider;

import com.intellij.psi.PsiDirectory;
import com.redare.dev.smart.generator.sdk.pojo.CodeTemplate;
import com.redare.dev.smart.generator.sdk.pojo.OnlFormHead;
import com.redare.dev.smart.generator.service.provider.filetype.LanguageFileType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * 文件提供者抽象类
 *
 */
public abstract class AbstractFileProvider {

    protected String outputPath;

    public AbstractFileProvider(String outputPath) {
        this.outputPath = outputPath;
    }

    public abstract void create(CodeTemplate template, OnlFormHead context, Map<String, Object> extraMap,String basePackage);

    protected PsiDirectory subDirectory(PsiDirectory psiDirectory, String subPath, Boolean isResources) {
        if (Objects.nonNull(isResources) && isResources) {
            psiDirectory = findResourcesDirectory(psiDirectory);
        }
        if (StringUtils.isNotEmpty(subPath)) {
            if ("/".equals(subPath.substring(0, 1))) {
                subPath = subPath.substring(1);
            }
            String[] subPathAttr = subPath.split("/");
            return createSubDirectory(psiDirectory, subPathAttr, 0);
        } else {
            return psiDirectory;
        }
    }

    /**
     * 创建子目录
     * @param psiDirectory
     * @param temp
     * @param level
     * @return
     */
    private PsiDirectory createSubDirectory(PsiDirectory psiDirectory, String[] temp, int level) {
        PsiDirectory subdirectory = psiDirectory.findSubdirectory(temp[level]);
        if (subdirectory == null) {
            subdirectory = psiDirectory.createSubdirectory(temp[level]);
        }
        if (temp.length != level + 1) {
            return createSubDirectory(subdirectory, temp, level + 1);
        }
        return subdirectory;
    }

    /**
     * 根据选择的package目录，找到resources目录
     * @param psiDirectory
     * @return
     */
    private PsiDirectory findResourcesDirectory(PsiDirectory psiDirectory) {

        PsiDirectory parentDirectory = psiDirectory.getParentDirectory();
        PsiDirectory iterator = psiDirectory.getParentDirectory();

        while (iterator != null && !iterator.getName().equals("main")) {
            iterator = iterator.getParentDirectory();
        }

        PsiDirectory resourcesDirectory = iterator == null ? null : iterator.findSubdirectory("resources");
        if (resourcesDirectory == null) {
            resourcesDirectory = parentDirectory.findSubdirectory("resources");
            if (resourcesDirectory == null) {
                resourcesDirectory = parentDirectory.createSubdirectory("resources");
            }
        }
        return resourcesDirectory;
    }

    protected void createFile(File directory, String fileName, String context, LanguageFileType fileType, String basePackage) {
//        PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(fileName, fileType, context);
//        // reformat class
//        if (psiFile instanceof PsiJavaFile) {
//            CodeStyleManager.getInstance(project).reformat(psiFile);
//            JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
//            styleManager.optimizeImports(psiFile);
//            styleManager.shortenClassReferences(psiFile);
//        }
//        psiDirectory.add(psiFile);
//        return psiFile;
        File file = new File(directory,fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileUtils.writeStringToFile(file,context);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}